package com.cnware.capture.model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.cnware.common.util.FolderUtil;
import com.cnware.common.util.SqliteDBUtil;


public class Project {
	
	private int id;
	private String name;
	private int index;
	private String folder;
	private String memo;
	private Workspace workspace;
	
	public static final String ID_FIELD = "id";
	public static final String NAME_FIELD = "project_name";
	public static final String INDEX_FIELD = "project_index";
	public static final String FOLDER_FIELD = "project_folder";
	public static final String MEMO_FIELD = "project_memo";
	
	
	private static final String DB_FILE_NAME = ".project";
	
	private ArrayList<Task> taskList  = new ArrayList<Task>();
	
	public Project(Workspace workspace) {
		this.workspace = workspace;
	}

	public Project(Workspace workspace, ResultSet rs) throws SQLException {
		this.workspace = workspace;
		
		this.id = rs.getInt(ID_FIELD);
		this.name = rs.getString(NAME_FIELD);
		this.index = rs.getInt(INDEX_FIELD);
		this.folder = rs.getString(FOLDER_FIELD);
		this.memo = rs.getString(MEMO_FIELD);
	}
	
	public void init(){
		FolderUtil.checkFolder(this.getWorkFolder(), true);
		loadTask();
	}
	
	private void loadTask(){
		Connection conn = null;
		try {
			String dbFilePath = this.workspace.getWorkForder() + "\\" + this.folder + "\\" + DB_FILE_NAME;
			conn = SqliteDBUtil.getConnection(dbFilePath);
			Statement stmt = conn.createStatement();
			stmt.setQueryTimeout(30);
			
			stmt.executeUpdate("create table if not exists task (" +
					"id INTEGER PRIMARY KEY," +
					"task_name TEXT NOT NULL," +
					"task_index INTEGER NOT NULL," +
					"task_folder TEXT NOT NULL," +
					"task_memo TEXT NOT NULL)");
			
			ResultSet rs = stmt.executeQuery("select * from task order by task_index desc");
			
			while(rs.next()){
				Task task = new Task(this,rs);
				this.taskList.add(task);
				task.init();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException ex) {
				// connection close failed.
				System.err.println(ex);
			}
		}
	}

	public String getWorkFolder(){
		return this.workspace.getWorkForder() + "\\" + this.folder;
	}
	
	public int taskLength(){
		return this.taskList.size();
	}
	
	//getters and setters
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String getFolder() {
		return folder;
	}

	public void setFolder(String folder) {
		this.folder = folder;
	}

	public String getMemo() {
		return memo;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}

	public Workspace getWorkspace() {
		return workspace;
	}

	public void setWorkspace(Workspace workspace) {
		this.workspace = workspace;
	}

	public ArrayList<Task> getTaskList() {
		return taskList;
	}

	
}
